/*
**************************************************************************************************
* @file    		w5500_conf.c
* @author  		WIZnet Software Team 
* @version 		V1.0
* @date    		2015-02-14
* @brief  		配置MCU，移植W5500程序需要修改的文件，配置W5500的MAC和IP地址
**************************************************************************************************
*/
#include <stdio.h> 
#include <string.h>
#include "w5500_conf.h"

WHT_W5500_Config_Info_t WHT_W5500_Config_Info;/*配置结构体*/


/**配置W5500的IP地址*/
void set_w5500_ip(WHT_W5500_IP_FROM_enum ip_from)
{
	WHT_W5500_Config_Info_t Info =
	{
		.lip = W5500_Default_IP,
		.sub = W5500_Defalut_Subnet,
		.gw  = W5500_Defalut_Gateway,
		.dns = W5500_Defalut_Dns_Server,
		.mac = W5500_Defalut_MAC,
		.sw_ver = W5500_SW_Version,
		.rip = W5500_Default_Remote_IP,
		.local_port = W5500_Default_Local_Port,
		.remote_port = W5500_Default_Remote_Port,
	};

	WHT_W5500_Config_Info = Info;
	if(ip_from==IP_FROM_DEFINE)
		printf("Default IP Info Config W5500\r\n");
	/*使用EEPROM存储的IP参数*/
	else if(ip_from==IP_FROM_EEPROM)
	{
		//待添加EEPROM
	}
	/*使用DHCP获取IP参数，需调用DHCP子函数*/		
	else if(ip_from==IP_FROM_DHCP)								
	{
		/*复制DHCP获取的配置信息到配置结构体*/
		if(dhcp_ok==1)
		{
			printf(" IP from DHCP\r\n");		 
			memcpy(WHT_W5500_Config_Info.lip,DHCP_GET.lip, 4);
			memcpy(WHT_W5500_Config_Info.sub,DHCP_GET.sub, 4);
			memcpy(WHT_W5500_Config_Info.gw,DHCP_GET.gw, 4);
			memcpy(WHT_W5500_Config_Info.dns,DHCP_GET.dns,4);
		}
		else
		{
			printf("DHCP Config IP Error\r\n");
			printf("Default IP Info Config W5500\r\n");
		}
	}
	/*将IP配置信息写入W5500相应寄存器*/	
	setSUBR(WHT_W5500_Config_Info.sub);
	setGAR(WHT_W5500_Config_Info.gw);
	setSIPR(WHT_W5500_Config_Info.lip);
	
	getSIPR (WHT_W5500_Config_Info.lip);			
	printf("W5500 IP:%u.%u.%u.%u\r\n", WHT_W5500_Config_Info.lip[0],WHT_W5500_Config_Info.lip[1],WHT_W5500_Config_Info.lip[2],WHT_W5500_Config_Info.lip[3]);
	getSUBR(WHT_W5500_Config_Info.sub);
	printf("W5500 SUB:%u.%u.%u.%u\r\n", WHT_W5500_Config_Info.sub[0],WHT_W5500_Config_Info.sub[1],WHT_W5500_Config_Info.sub[2],WHT_W5500_Config_Info.sub[3]);
	getGAR(WHT_W5500_Config_Info.gw);
	printf("W5500 GW:%u.%u.%u.%u\r\n", WHT_W5500_Config_Info.gw[0],WHT_W5500_Config_Info.gw[1],WHT_W5500_Config_Info.gw[2],WHT_W5500_Config_Info.gw[3]);
}
/**	配置W5500的MAC地址*/
void set_w5500_mac(uint8_t* mac)
{
	memcpy(WHT_W5500_Config_Info.mac, mac, 6);
	setSHAR(WHT_W5500_Config_Info.mac);
	memcpy(DHCP_GET.mac, mac, 6);
}


static uint8_t Temp_Buffer[256];
/**
*@brief		写入一个8位数据到W5500
*@param		addrbsb: 写入数据的地址
*@param   data：写入的8位数据
*/
void IINCHIP_WRITE( uint32_t addrbsb,  uint8_t data)
{
	Temp_Buffer[0] = (addrbsb & 0x00FF0000)>>16;
	Temp_Buffer[1] = (addrbsb & 0x0000FF00)>>8;
	Temp_Buffer[2] = (addrbsb & 0x000000F8)+4;
	Temp_Buffer[3] = data;
	WHT_W5500_Driver.WHT_Write_Data(Temp_Buffer, 4);
}
/**
*@brief		从W5500读出一个8位数据
*@param		addrbsb: 写入数据的地址
*@param   data：从写入的地址处读取到的8位数据
*/
uint8_t IINCHIP_READ(uint32_t addrbsb)
{
	Temp_Buffer[0] = (addrbsb & 0x00FF0000)>>16;
	Temp_Buffer[1] = (addrbsb & 0x0000FF00)>>8;
	Temp_Buffer[2] = (addrbsb & 0x000000F8);
	Temp_Buffer[3] = 0;
	WHT_W5500_Driver.WHT_Read_Data(Temp_Buffer, Temp_Buffer, 4);
    return Temp_Buffer[3];
}
/**
*@brief		向W5500写入len字节数据
*@param		addrbsb: 写入数据的地址
*@param   buf：写入字符串
*@param   len：字符串长度
*@return	len：返回字符串长度
*/
uint16_t wiz_write_buf(uint32_t addrbsb,uint8_t* buf,uint16_t len)
{
	if (len > sizeof(Temp_Buffer) - 3)
		return 0;
	Temp_Buffer[0] = (addrbsb & 0x00FF0000)>>16;
	Temp_Buffer[1] = (addrbsb & 0x0000FF00)>>8;
	Temp_Buffer[2] = (addrbsb & 0x000000F8)+4;
	memcpy(&Temp_Buffer[3], buf, len);
	WHT_W5500_Driver.WHT_Write_Data(Temp_Buffer, len+3);                     
	return len;
}
/**
*@brief		从W5500读出len字节数据
*@param		addrbsb: 读取数据的地址
*@param 	buf：存放读取数据
*@param		len：字符串长度
*@return	len：返回字符串长度
*/
uint16_t wiz_read_buf(uint32_t addrbsb, uint8_t* buf,uint16_t len)
{
	if (len > sizeof(Temp_Buffer) - 3)
		return 0;
	Temp_Buffer[0] = (addrbsb & 0x00FF0000)>>16;
	Temp_Buffer[1] = (addrbsb & 0x0000FF00)>>8;
	Temp_Buffer[2] = (addrbsb & 0x000000F8);
	WHT_W5500_Driver.WHT_Read_Data(Temp_Buffer, Temp_Buffer, len+3);
	memcpy(buf, &Temp_Buffer[3], len);
    return len;
}
